﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FinanSoft.Entities;
using FinanSoft.Utilities;
using System.Data.SqlClient;
using FinanSoft.BusinessLayer;

namespace FinanSoft.DataLayer
{
    class OrdenCompraDataManager
    {
        #region Fields
        /// <summary>
        /// Atributo privado del tipo de la clase. Unica instancia de esta clase que existira
        /// </summary>
        private static OrdenCompraDataManager _instance;
        #endregion

        #region Constructors
        /// <summary>
        /// Constructor privado de la clase para permitir la implementacion del patron singleton
        /// </summary>
        private OrdenCompraDataManager()
        {

        }
        #endregion

        #region Methods
        /// <summary>
        /// Metodo que obtienen la instancia existente de la clase. Si esta aun no ha sido instanciada, la instancia.
        /// </summary>
        public static OrdenCompraDataManager GetInstance()
        {
            if (_instance == null)
                _instance = new OrdenCompraDataManager();
            return _instance;
        }


        /// <summary>
        /// Se encarga de registrar un nuevo articulo documento a la base de datos
        /// </summary>
        /// <param name="newArticuleDocument"> El objeto articuloDocumento a registrar en la base</param>
        /// <returns>El entero que representa el Id del articulo registrado registrada</returns>
        public int RegisterArticleDocument(ArticuloDocumento newArticleDocument)
        {
            try
            {
                AppConfigReader appConfig = AppConfigReader.GetInstance();
                string spName = Constants.SP_NAME_ADD_DOCUMENT_ARTICLES;

                SqlParameter[] parameters = {new SqlParameter(Constants.SP_COLUMN_ADD_DOCUMENT_ARTICLES_ID_DOCUMENT, newArticleDocument.IdDocumentoContable),
                                             new SqlParameter(Constants.SP_COLUMN_ADD_DOCUMENT_ARTICLES_ID_STORE, newArticleDocument.IdBodega),
                                             new SqlParameter(Constants.SP_COLUMN_ADD_DOCUMENT_ARTICLES_ID_ARTICLE, newArticleDocument.IdArticulo),
                                             new SqlParameter(Constants.SP_COLUMN_ADD_DOCUMENT_ARTICLES_DESCRIPTION, newArticleDocument.Descripcion),
                                             new SqlParameter(Constants.SP_COLUMN_ADD_DOCUMENT_ARTICLES_CANTIDAD, newArticleDocument.Cantidad),
                                             new SqlParameter(Constants.SP_COLUMN_ADD_DOCUMENT_ARTICLES_PRECIO, newArticleDocument.PrecioCompraUnitario),
                                            new SqlParameter(Constants.SP_COLUMN_ADD_DOCUMENT_ARTICLES_ID_TIPO_IMPUESTO,newArticleDocument.IdTipoImpuesto)};

                SqlDataHelper dataHelper = new SqlDataHelper(appConfig.GetConnectionString(Constants.DB_CONN));
                SqlDataReader result = dataHelper.ExecuteStoredProcedure(spName, parameters);

                if (result.HasRows)
                {
                    result.Read();
                    int idDocumentoContable = Convert.ToInt32(result[Constants.SP_COLUMN_ADD_DOCUMENT_ARTICLES_ID_DOCUMENT]);
                    return idDocumentoContable;
                }
                return -1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }


        /// <summary>
        /// Obtiene todas los articulos documentos de la base de datos
        /// </summary>
        /// <returns>El objeto lista de articulos documentos</returns>
        public List<ArticuloDocumento> GetArticleDocuments()
        {
            List<ArticuloDocumento> articleDocumentList;

            try
            {
                articleDocumentList = new List<ArticuloDocumento>();
                AppConfigReader appConfig = AppConfigReader.GetInstance();
                string spName = Constants.SP_NAME_GET_DOCUMENT_ARTICLES;
                SqlParameter[] parameters = new SqlParameter[0];
                SqlDataHelper dataHelper = new SqlDataHelper(appConfig.GetConnectionString(Constants.DB_CONN));
                SqlDataReader result = dataHelper.ExecuteStoredProcedure(spName, parameters);
                while (result.Read())
                {
                    int idDocument = System.Convert.ToInt32(result[Constants.SP_COLUMN_GET_DOCUMENT_ARTICLE_ID_DOCUMENT]);
                    int idStore = System.Convert.ToInt32(result[Constants.SP_COLUMN_GET_DOCUMENT_ARTICLE_ID_STORE]);
                    int idArticle = System.Convert.ToInt32(result[Constants.SP_COLUMN_GET_DOCUMENT_ARTICLE_ID_ARTICLE]);
                    String description = (string)result[Constants.SP_COLUMN_GET_DOCUMENT_ARTICLE_DESCRIPTION];
                    int cantidad = System.Convert.ToInt32(result[Constants.SP_COLUMN_GET_DOCUMENT_ARTICLE_CANTIDAD]);
                    Double price = System.Convert.ToDouble(result[Constants.SP_COLUMN_GET_DOCUMENT_ARTICLE_PRECIO]);
                    int idTipoImpuesto = System.Convert.ToInt32(result[Constants.SP_COLUMN_GET_DOCUMENT_ARTICLE_ID_TIPO_IMPUESTO]);


                    articleDocumentList.Add(new ArticuloDocumento(idDocument, idStore, idArticle, description, cantidad, price, idTipoImpuesto));
                }
                dataHelper.CloseReader();
                return articleDocumentList;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        #endregion Methods
    }
}
